package com.singfan.core.dao.mongo.impl;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.singfan.common.nosql.mongo.MongoDaoBase;
import com.singfan.core.dao.database.model.constant.GenderType;
import com.singfan.core.dao.mongo.IHairstyleLocationDao;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: hansonghe
 * Date: 2015-08-25 14:28
 * Desc:
 */
@Repository
public class HairstyleLocationDaoImpl extends MongoDaoBase implements IHairstyleLocationDao {
    private static final String COLLECTION_NAME = "hairstyle";
    @Override
    public void createHairstyleLocation(long hairstyleId, int cityId, int businessCircleId, int gender, double longitude, double latitude) {
        DBObject dbo = new BasicDBObject();
        dbo.put("_id", new ObjectId());
        dbo.put("hairstyle_id", hairstyleId);
        dbo.put("city_id", cityId);
        dbo.put("business_circle_id", businessCircleId);
        dbo.put("gender", gender);
        dbo.put("location", new double[]{longitude,latitude});
        insertObj(COLLECTION_NAME, dbo);
    }

    @Override
    public List<Long> findHairstyleNearbyLocation(int cityId, int businessCircleId, int gender, double longitude, double latitude, int pageIndex, int pageSize) {
//        Document searchDoc = new Document();
//        if (businessCircleId == 0){
//            searchDoc.put("city_id", cityId);
//        }
//        else {
//            searchDoc.put("business_circle_id", businessCircleId);
//        }
//        searchDoc.put("location", new Document("$near", new Double[]{longitude, latitude}));
//        List<Document> hairstyleDocList = selectList(COLLECTION_NAME, searchDoc, pageIndex, pageSize);
//        List<Long> hairstyleIdList = new ArrayList<>(hairstyleDocList.size());
//        for (Document hairstyleDoc : hairstyleDocList){
//            hairstyleIdList.add(hairstyleDoc.getLong("hairstyle_id"));
//        }
//
//        return hairstyleIdList;

        BasicDBObject searchObj = new BasicDBObject();
        if (businessCircleId == 0){
            searchObj.put("city_id", cityId);
        }
        else {
            searchObj.put("business_circle_id", businessCircleId);
        }
        if (GenderType.isGenderType(gender)){
            searchObj.put("gender", gender);
        }
        searchObj.put("location", new BasicDBObject("$near", new double[]{longitude, latitude}));
        List<DBObject> hairstyleObjList = selectList(COLLECTION_NAME, searchObj, pageIndex, pageSize);
        List<Long> hairstyleIdList = new ArrayList<>(hairstyleObjList.size());
        for (DBObject barberObj : hairstyleObjList){
            hairstyleIdList.add(Long.valueOf(barberObj.get("hairstyle_id").toString()));
        }

        return hairstyleIdList;
    }

    @Override
    protected String getMongoDatabaseName()  {
        return "singfan_location_0";
    }
}
